package com.biz.sourceAndTargetbiz;

import java.sql.SQLException;
import java.util.ArrayList;

import admin.mode.AdminMode;
import base.Power;
import base.tools.GetTime;
import cn.schina.dbfw.dao.impl.DBObjectParamsDao;
import cn.schina.dbfw.dao.impl.DBObjectsParam;
import cn.schina.dbfw.dao.impl.DBWithDbfw;
import cn.schina.dbfw.dao.impl.DBWithDbfwDao;
import cn.schina.dbfw.dao.impl.DateParam;
import cn.schina.dbfw.dao.impl.Dbfw;
import cn.schina.dbfw.dao.impl.QueryParam;
import cn.schina.dbfw.dao.impl.QueryParamType;
import cn.schina.dbfw.dao.impl.QueryParamsDao;
import cn.schina.dbfw.dao.impl.SensiObjAnalysisDao;
import cn.schina.dbfw.dao.impl.SensiObjAnalysisResult;

public class SensitivityObjectBiz {
	ArrayList<DBWithDbfw> dbwithdbfwArray = new ArrayList<DBWithDbfw>();
	ArrayList<QueryParam> queryParams = new ArrayList<QueryParam>();
	ArrayList<DBObjectsParam> objectParams = new ArrayList<DBObjectsParam>();

	public ArrayList<DBWithDbfw> getValue(Power power, AdminMode admin) {
		DBWithDbfwDao _dbwithdbfw;
		_dbwithdbfw = new DBWithDbfwDao();
		QueryParamsDao _queryparamdao;
		_queryparamdao = new QueryParamsDao();
		DBObjectParamsDao _objectparamdao;
		_objectparamdao = new DBObjectParamsDao();
		ArrayList<DBObjectsParam> objectParams1 = new ArrayList<DBObjectsParam>();
		try {
			// if (dbwithdbfwArray.size() == 0) {
			dbwithdbfwArray = _dbwithdbfw.getDBWithDbfw();
			// }
			// if (queryParams.size() == 0) {
			queryParams = _queryparamdao.getQueryParams();
			// }
			if (objectParams.size() == 0) {
				objectParams = _objectparamdao.getDBObjectParams();
			}

			for (int i = 0; i < 20; i++) {
				objectParams1.add(objectParams.get(i));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		power.setAttribute("dbtable", createDbTable(dbwithdbfwArray, power));
		power
				.setAttribute("dbfwtable", createDBFWTable(dbwithdbfwArray,
						power));
		power.setAttribute(QueryParamType.CLIENTMAC.valuename, createOther(
				queryParams, power, QueryParamType.CLIENTMAC));
		power.setAttribute(QueryParamType.CLIENT_IP.valuename, createOther(
				queryParams, power, QueryParamType.CLIENT_IP));
		power.setAttribute(QueryParamType.DB_USER.valuename, createOther(
				queryParams, power, QueryParamType.DB_USER));
		power.setAttribute(QueryParamType.PROGRAM.valuename, createOther(
				queryParams, power, QueryParamType.PROGRAM));
		power.setAttribute(QueryParamType.PROGRAM_NM.valuename, createOther(
				queryParams, power, QueryParamType.PROGRAM_NM));
		power.setAttribute(QueryParamType.DBOBJECTPARAMS.valuename,
				createDbObject(objectParams1, power,
						QueryParamType.DBOBJECTPARAMS));
		return dbwithdbfwArray;
	}

	public ArrayList<SensiObjAnalysisResult> getResult(Power power,
			AdminMode admin) {

		ArrayList<SensiObjAnalysisResult> sensiResultArray = new ArrayList<SensiObjAnalysisResult>();
		SensiObjAnalysisDao _sensianalysisdao;
		_sensianalysisdao = new SensiObjAnalysisDao();
		DBWithDbfw p_dbwithdbfw = new DBWithDbfw();
		for (int i = 0; i < dbwithdbfwArray.size(); i++) {
			DBWithDbfw withDbfw = dbwithdbfwArray.get(i);
			if (power.getParameter("db").equals(withDbfw.getId() + "")) {
				p_dbwithdbfw = withDbfw;
				ArrayList<Dbfw> list = new ArrayList<Dbfw>();
				for (int j = 0; j < p_dbwithdbfw.getDbfwArray().size(); j++) {
					Dbfw dbfw = p_dbwithdbfw.getDbfwArray().get(j);
					String[] temp = power.getParameter("dbfw").split(",");
					for (int k = 0; k < temp.length; k++) {
						if (temp[k].equals(dbfw.getInstid() + "")) {
							list.add(dbfw);
						}
					}
				}
				p_dbwithdbfw.setDbfwArray(list);
			}
		}
		ArrayList<QueryParam> p_queryParams = new ArrayList<QueryParam>();
		DBObjectsParam p_objectParam = new DBObjectsParam();
		DateParam p_date = new DateParam();
		for (int i = 0; i < queryParams.size(); i++) {
			QueryParam param = queryParams.get(i);
			if (param.getParam_code() == QueryParamType.CLIENT_IP.id) {
				String[] temp = power.getParameter(
						QueryParamType.CLIENT_IP.valuename).split(",");
				for (int j = 0; j < temp.length; j++) {
					if (temp[j].equals(param.getParam_value())) {
						p_queryParams.add(param);
					}
				}
			}
			if (param.getParam_code() == QueryParamType.CLIENTMAC.id) {
				String[] temp = power.getParameter(
						QueryParamType.CLIENTMAC.valuename).split(",");
				for (int j = 0; j < temp.length; j++) {
					if (temp[j].equals(param.getParam_value())) {
						p_queryParams.add(param);
					}
				}
			}
			if (param.getParam_code() == QueryParamType.DB_USER.id) {
				String[] temp = power.getParameter(
						QueryParamType.DB_USER.valuename).split(",");
				for (int j = 0; j < temp.length; j++) {
					if (temp[j].equals(param.getParam_value())) {
						p_queryParams.add(param);
					}
				}
			}
			if (param.getParam_code() == QueryParamType.PROGRAM_NM.id) {
				String[] temp = power.getParameter(
						QueryParamType.PROGRAM_NM.valuename).split(",");
				for (int j = 0; j < temp.length; j++) {
					if (temp[j].equals(param.getParam_value())) {
						p_queryParams.add(param);
					}
				}
			}

		}
		for (int i = 0; i < objectParams.size(); i++) {
			DBObjectsParam dbObjectsParam = objectParams.get(i);
			if (power.getParameter("dbobjectparams").equals(
					dbObjectsParam.getObjectname())) {
				p_objectParam = dbObjectsParam;
			}
		}
		if (power.getParameter("time").equals(GetTime.LASTMONTH.name)) {
			p_date=GetTime.time(GetTime.LASTMONTH);
		}else if (power.getParameter("time").equals(GetTime.THISMONTH.name)) {
			p_date=GetTime.time(GetTime.THISMONTH);
		}else if (power.getParameter("time").equals(GetTime.LASTWEEK.name)) {
			p_date=GetTime.time(GetTime.LASTWEEK);
		}else if (power.getParameter("time").equals(GetTime.THISWEEK.name)) {
			p_date=GetTime.time(GetTime.THISWEEK);
		}else if (power.getParameter("time").equals(GetTime.YESTERDAY.name)) {
			p_date=GetTime.time(GetTime.YESTERDAY);
		}else if (power.getParameter("time").equals(GetTime.TODAY.name())) {
			p_date=GetTime.time(GetTime.TODAY);
		}else {
			p_date.setStartDate(power.getParameter("beforetime"));
			p_date.setEndDate(power.getParameter("lasttime"));
		}
		try {
			sensiResultArray = _sensianalysisdao.getSensiObjAnalysis(
					p_dbwithdbfw, p_queryParams, p_objectParam, p_date);
			power.setAttribute("DBWithDbfw", p_dbwithdbfw);
			power.setAttribute("DBObjectsParam", p_objectParam);
			power.setAttribute("QueryParam", p_queryParams);
			power.setAttribute("DateParam", p_date);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return sensiResultArray;
	}

	private String createDbTable(ArrayList<DBWithDbfw> dbwithdbfwArray,
			Power power) {
		StringBuffer table = new StringBuffer();
		table
				.append("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr>");
		if ((dbwithdbfwArray.size() % 5) == 0) {
			getDbRedioTable(dbwithdbfwArray, power, table);
		} else {
			getDbRedioTable(dbwithdbfwArray, power, table);
			for (int i = 0; i < (5 - dbwithdbfwArray.size() % 5); i++) {
				table.append("<td>&nbsp;</td>");
			}
		}
		table.append("</tr></table>");
		return table.toString();
	}

	private String createDbObject(ArrayList<DBObjectsParam> dbObjectsParams,
			Power power, QueryParamType paramType) {
		StringBuffer table = new StringBuffer();
		table.append("<table width=\"100%\" border=\"0\""
				+ " cellspacing=\"0\" cellpadding=\"0\"><tr>");
		if (dbObjectsParams.size() % 5 == 0) {
			getDbObjectTable(dbObjectsParams, power, table, paramType);
		} else {
			getDbObjectTable(dbObjectsParams, power, table, paramType);
			for (int i = 0; i < (5 - dbObjectsParams.size() % 5); i++) {
				table.append("<td>&nbsp;</td>");
			}
		}
		table.append("</tr></table>");
		return table.toString();
	}

	private String createOther(ArrayList<QueryParam> queryParams, Power power,
			QueryParamType paramType) {
		StringBuffer table = new StringBuffer();
		table.append("<table width=\"100%\" border=\"0\""
				+ " cellspacing=\"0\" cellpadding=\"0\"><tr>");
		int sum = 0;
		for (int i = 0; i < queryParams.size(); i++) {
			if (paramType.id == queryParams.get(i).getParam_code()) {
				sum++;
			}
		}
		if (sum % 5 == 0) {
			getOtherInfoTable(queryParams, power, table, paramType);
		} else {
			getOtherInfoTable(queryParams, power, table, paramType);
			for (int i = 0; i < (5 - (sum % 5)); i++) {
				table.append("<td>&nbsp;</td>");
			}
		}
		table.append("</tr></table>");
		return table.toString();
	}

	private String createDBFWTable(ArrayList<DBWithDbfw> dbwithdbfwArray,
			Power power) {
		StringBuffer table = new StringBuffer();
		table
				.append("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr>");
		if ((dbwithdbfwArray.size() % 5) == 0) {
			getRedioTable(dbwithdbfwArray, power, table);
		} else {
			getRedioTable(dbwithdbfwArray, power, table);
			for (int i = 0; i < (5 - dbwithdbfwArray.size() % 5); i++) {
				table.append("<td>&nbsp;</td>");
			}
		}
		table.append("</tr></table>");
		return table.toString();
	}

	private static void getDbRedioTable(ArrayList<DBWithDbfw> dbwithdbfwArray,
			Power power, StringBuffer table) {
		for (int i = 0; i < dbwithdbfwArray.size(); i++) {
			if (power.getParameter("db").equals(
					dbwithdbfwArray.get(i).getId() + "")) {
				table.append("<td class=\"checkbox-type\"><input name=\"db\""
						+ " type=\"radio\"  checked=\"checked\" "
						+ "style=\"vertical-align: auto\" value=\""
						+ dbwithdbfwArray.get(i).getId()
						+ "\"  onclick=\"optDelete()\">"
						+ dbwithdbfwArray.get(i).getName() + "</td>");
			} else {
				table.append("<td class=\"checkbox-type\"><input name=\"db\""
						+ " type=\"radio\"   "
						+ "style=\"vertical-align: auto\" value=\""
						+ dbwithdbfwArray.get(i).getId()
						+ "\" onclick=\"optDelete()\">"
						+ dbwithdbfwArray.get(i).getName() + "</td>");
			}
			if ((i + 1) % 5 == 0) {
				table.append("</tr><tr>");
			}
		}
	}

	private static void getRedioTable(ArrayList<DBWithDbfw> dbwithdbfwArray,
			Power power, StringBuffer table) {
		for (int i = 0; i < dbwithdbfwArray.size(); i++) {
			if (power.getParameter("db").equals(
					dbwithdbfwArray.get(i).getId() + "")) {
				ArrayList<Dbfw> list = dbwithdbfwArray.get(i).getDbfwArray();
				for (int j = 0; j < list.size(); j++) {
					if (power.getParameter("dbfw").equals(
							list.get(j).getInstid() + "")) {
						table
								.append("<td class=\"checkbox-type\"><input name=\"dbfw\""
										+ " type=\"checkbox\"  checked=\"checked\" "
										+ "style=\"vertical-align: auto\" value=\""
										+ list.get(j).getInstid()
										+ "\"   >"
										+ list.get(j).getName() + "</td>");
					} else {

						table
								.append("<td class=\"checkbox-type\"><input name=\"dbfw\""
										+ " type=\"checkbox\"   "
										+ "style=\"vertical-align: auto\" checked=\"checked\" value=\""
										+ list.get(j).getInstid()
										+ "\"  >"
										+ list.get(j).getName() + "</td>");

					}
					if ((j + 1) % 5 == 0) {
						table.append("</tr><tr>");
					}
				}
			}
		}
	}

	private static void getOtherInfoTable(ArrayList<QueryParam> queryParams,
			Power power, StringBuffer table, QueryParamType paramType) {
		int sum = 0;
		for (int i = 0; i < queryParams.size(); i++) {
			QueryParam param = queryParams.get(i);
			if (param.getParam_code() == paramType.id) {
				table.append("<td class=\"checkbox-type\"><input name=\""
						+ paramType.valuename + "\""
						+ " type=\"checkbox\"  checked=\"checked\" "
						+ "style=\"vertical-align: auto\" value=\""
						+ param.getParam_value() + "\"   >"
						+ param.getParam_value() + "</td>");
				sum++;
				if (sum % 5 == 0) {
					table.append("</tr><tr>");
				}
			}
		}
	}

	private static void getDbObjectTable(
			ArrayList<DBObjectsParam> dbObjectsParams, Power power,
			StringBuffer table, QueryParamType paramType) {
		int sum = 0;
		for (int i = 0; i < dbObjectsParams.size(); i++) {
			DBObjectsParam param = dbObjectsParams.get(i);
			if (i == 0) {
				table.append("<td class=\"checkbox-type\"><input name=\""
						+ paramType.valuename + "\""
						+ " type=\"radio\" checked=\"checked\" "
						+ "style=\"vertical-align: auto\" value=\""
						+ param.getObjectname() + "\"   >"
						+ param.getObjectname() + "</td>");
			} else {
				table.append("<td class=\"checkbox-type\"><input name=\""
						+ paramType.valuename + "\"" + " type=\"radio\"  "
						+ "style=\"vertical-align: auto\" value=\""
						+ param.getObjectname() + "\"   >"
						+ param.getObjectname() + "</td>");
			}
			sum++;
			if (sum % 5 == 0) {
				table.append("</tr><tr>");
			}
		}
	}
}
